package net.dfrz.oa.action.support;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 所有action继承他
 * @author vovh
 *
 * @param <T>
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 返回request对象
	 * @return
	 */
	public HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 返回response对象
	 */
	public HttpServletResponse getResponse(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		return response;
	}
	
	/**
	 * 返回session对象
	 * @return
	 */
	public HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * 将date  type===yyyy-MM-dd hh:mm:ss类型转化成String类型
	 */
	public String dateToString(Date fromDate,String type){
		SimpleDateFormat sdf = new SimpleDateFormat(type);
		String toString = sdf.format(fromDate);
		return toString;
	}
	
	/**
	 * 将String -->date
	 * @throws ParseException 
	 */
	public Date stringToDate(String fromString,String type) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(type);
		Date toDate = sdf.parse(fromString);
		return toDate;
	}
}
